using JT808Protocol.Common;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy
{
    /// <summary>
    /// 终端鉴权
    /// </summary>
    public class DrAuthentication_0x0102 : IJT808Body
    {
        /// <summary>
        /// 鉴权码长度
        /// </summary>
        [JsonPropertyName("鉴权码长度")] public byte AkeyLength { get; private set; }

        /// <summary>
        /// 鉴权码内容
        /// </summary>
        [JsonPropertyName("鉴权码")] public string Akey { get; private set; }

        /// <summary>
        /// 15 个字节长度
        /// </summary>
        [JsonPropertyName("Imei")] public string Imei { get; private set; }

        /// <summary>
        /// 软件版本号，20 个字节
        /// </summary>
        [JsonPropertyName("软件版本号")] public string SoftVer { get; private set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2019)
            {
                // 鉴权码 长度
                AkeyLength = source[index];

                //鉴权码
                index++;
                Akey = ParseHelper.Gb2312String(ref source, index, AkeyLength);

                // 终端 imei
                index += AkeyLength;
                Imei = ParseHelper.AsciiString(ref source, index, 15);

                // 软件版本
                index += 15;
                SoftVer = ParseHelper.Gb2312String(ref source, index, 20);
            }
            else if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2013)
            {
                //鉴权码
                Akey = ParseHelper.Gb2312String(ref source, index, source.Length - index - 2);
            }

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new NotImplementedException();
        }
    }
}